//
//  TTCategory1Cell.m
//  02-分类界面
//
//  Created by peironggao on 2017/10/28.
//  Copyright © 2017年 gaopeirong. All rights reserved.
//

#import "TTCategory1Cell.h"
#import "TTCactegoryModel.h"

#define LINE_COLOR [UIColor colorWithRed:15/255.0 green:15/255.0 blue:15/255.0 alpha:0.5]

@interface TTCategory1Cell ()

@property (nonatomic, weak) UIView *lineTop;

@property (nonatomic, weak) UIView *lineBottom;

@property (nonatomic, weak) UILabel *categoryLabel;

@end

@implementation TTCategory1Cell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIView *lineTop = [[UIView alloc] init];
    [self.contentView addSubview:lineTop];
    lineTop.backgroundColor = LINE_COLOR;
    _lineTop = lineTop;
    
    UIView *lineBottom = [[UIView alloc] init];
    [self.contentView addSubview:lineBottom];
    lineBottom.backgroundColor = LINE_COLOR;
    lineBottom.hidden = YES;
    _lineBottom = lineBottom;
    
    UILabel *categoryLabel = [[UILabel alloc] init];
    categoryLabel.text = @"创意设计";
    [self.contentView addSubview:categoryLabel];
    categoryLabel.textColor = [UIColor blackColor];
    categoryLabel.font = CELL_TITLE_FONT;
    _categoryLabel = categoryLabel;
    
    [self setCategorySelected:NO];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _lineTop.frame = CGRectMake(0, 0, self.contentView.bounds.size.width, 0.5);
    _lineBottom.frame = CGRectMake(0, self.contentView.bounds.size.height - 0.5,
                                   self.contentView.bounds.size.width, 0.5);
    [_categoryLabel sizeToFit];
    _categoryLabel.center = CGPointMake(0.5 * self.contentView.bounds.size.width, 0.5 * self.contentView.bounds.size.height);
}

- (void)setModel:(TTCactegoryModel *)model
{
    _model = model;
    [self setCategorySelected:model.selected];
    _lineBottom.hidden = !model.isLast;
    _categoryLabel.text = model.name;
//    [_categoryLabel size];
    _categoryLabel.center = CGPointMake(0.5 * self.contentView.bounds.size.width, 0.5 * self.contentView.bounds.size.height);
}

- (void)setCategorySelected:(BOOL)selected
{
    self.contentView.backgroundColor = selected ? CATEGORY1_SELECTED_BG_COLOR : CATEGORY1_DEFAULT_BG_COLOR;
}

@end
